package com.os.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.os.dao.DeskDaoS;
import com.os.model.DeskS;
import com.os.model.FoodS;
import com.os.util.JdbcConUtilM;

public class DeskDaoImplS implements DeskDaoS {

	@Override
	public int selectDeskCountS() {
		int flag = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select  count(oid) scount from o_desk");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			ResultSet re = pst.executeQuery();
			if(re.next()){
				flag = re.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return flag;
	}

	@Override
	public List<DeskS> selectDeskS(int page, int rows) {
		List<DeskS> list = new ArrayList<DeskS>();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from o_desk");
		sql.append(" limit ?,?");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, (page-1)*rows);
			pst.setInt(2, rows);
			ResultSet re = pst.executeQuery();
			while(re.next()){
				DeskS d = new DeskS();
				d.setDid(re.getInt("did"));
				d.setFnum(re.getInt("fnum"));
				d.setFoodlist(re.getString("foodlist"));
				d.setFstate(re.getInt("fstate"));
				d.setFtime(re.getString("ftime"));
				d.setOid(re.getInt("oid"));
				d.setWid(re.getInt("wid"));
				list.add(d);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return list;
	}

	@Override
	public boolean addDeskS(int did, String foodlist, int wid,int fnum,String ftime) {
		boolean flag = false;
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO o_desk (did,foodlist,fstate,wid,fnum,ftime) ");
		sql.append(" VALUES (?,?,-1,?,?,?)");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, did);
			pst.setString(2, foodlist);
			pst.setInt(3, wid);
			pst.setInt(4, fnum);
			pst.setString(5, ftime);
			int i = pst.executeUpdate();
			if(i>0){
				flag = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return flag;
	}

	@Override
	public int selectFnum(int oid) {
		int fnum = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select fnum from o_desk where oid=?");
		Connection con = JdbcConUtilM.getCon();
		PreparedStatement pst = null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, oid);
			ResultSet re = pst.executeQuery();
			while(re.next()){
				fnum = re.getInt("fnum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JdbcConUtilM.closePstAndCon(pst, con);
		return fnum;
	}
}
